package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.OderIofo;
import com.itheima.service.OrderService;
import com.itheima.utils.DateUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/order")
public class OrderController {
  @Reference
  private OrderService orderService;

  @RequestMapping("/findPage")
  public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
    PageResult page =null;
    try {
      page = orderService.findPage(queryPageBean);

    }catch (Exception e){
      return null;
    }
    return page;
  }


  @RequestMapping("/deleteOrder")
  public Result deleteOrder(Integer id){

    try {
      orderService.deleteOrder(id);
    return new Result(true, MessageConstant.DELETE_ORDER_SUCCESS);
    }catch (Exception e){
      return new Result(false, MessageConstant.DELETE_ORDER_FAIL);
    }

  }


  @RequestMapping("/add")
  public Result add(@RequestBody OderIofo oderIofo, Integer[] setmealIds) {
    try {
      Result result = orderService.add(oderIofo, setmealIds);
      return result;
    } catch (Exception e) {
      //新增失败
      return new Result(false, MessageConstant.ADD_ORDER_FAIL);
    }

  }


  @RequestMapping("/findById")
  public Result findById(Integer id) {
    try {
      OderIofo oderIofo = orderService.findBy(id);
      return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS,oderIofo);
    } catch (Exception e) {
      //新增失败
      return new Result(false, MessageConstant.QUERY_ORDER_FAIL);
    }

  }


  @RequestMapping("/edit")
  public Result edit(@RequestBody OderIofo oderIofo) {
    try {
      orderService.edit(oderIofo);
      return new Result(true, MessageConstant.EDIT_ORDER_SUCCESS);
    } catch (Exception e) {
      //新增失败
      return new Result(false, MessageConstant.EDIT_ORDER_FAIL);
    }

  }
  @RequestMapping("/findByDate")
  public Result findByDate(@RequestBody String[] date) {
    try{
      Map<String,Object> byDate = orderService.findByDate(date[0], date[1]);

      return  new Result(true,"成功获取数据",byDate);
    }catch (Exception e){

      return new Result(false,"获取数据失败");

    }


  }
  @RequestMapping("/getsetmealcount")
  public Result getSetmealCount() {

    List<Map<String,Object>> list = orderService.getSetmealCount();
    Map<String, Object> map = new HashMap<>();
    map.put("setmealCount", list);
    List<String> setmealNames = new ArrayList<>();
    for (Map<String, Object> m : list) {
      String name = (String) m.get("name");
      setmealNames.add(name);
    }
    map.put("setmealName", setmealNames);
    return new Result(true, MessageConstant.GET_SETMEAL_COUNT_REPORT_SUCCESS, map);
  }

  @RequestMapping("/change")
  public Result change(Integer id){
    try {
      orderService.change(id);
    }catch (Exception e){
      return new Result(false,MessageConstant.CHANGE_STATUS_FAIL);
    }
    return new Result(true,MessageConstant.CHANGE_STATUS_SUCCESS);
  }

}
